var map, shanghaiSpatialRef, initExtent
var closePanel, resetBtnPosition
var initAroundSearchPanel
require([
    "esri/map", "dojo/_base/connect",
    "esri/dijit/FeatureTable",
    "esri/symbols/PictureMarkerSymbol",

    "dijit/Menu",
    "dijit/MenuItem",
    "dijit/MenuSeparator",
    "esri/dijit/HomeButton",

    "esri/layers/ArcGISDynamicMapServiceLayer",
    "esri/layers/ArcGISTiledMapServiceLayer",
    "esri/layers/FeatureLayer", "esri/layers/GraphicsLayer",

    "esri/SpatialReference",

    "esri/graphic",
    "esri/InfoTemplate",
    "esri/geometry/Extent", "esri/geometry/Polygon",
    "esri/geometry/Point",
    "dojo/domReady!"
], function (
    Map, connect, FeatureTable,
    PictureMarkerSymbol,
    Menu, MenuItem, MenuSeparator, HomeButton,
    ArcGISDynamicMapServiceLayer,
    ArcGISTiledMapServiceLayer, FeatureLayer, GraphicsLayer,
    SpatialReference, Graphic,
    InfoTemplate, Extent, Polygon, Point) {

    // esriConfig.defaults.io.proxyUrl = "http://10.87.94.52/DotNet/proxy.ashx"
    // esriConfig.defaults.io.alwaysUseProxy = true

    // esriConfig.defaults.map.panDuration = 250;
    // esriConfig.defaults.map.panRate = 10;
    // esriConfig.defaults.map.zoomDuration = 250;
    // esriConfig.defaults.map.zoomRate = 10;

    shanghaiSpatialRef = new SpatialReference({
        "wkt": 'PROJCS["shanghaicity",' + 'GEOGCS["GCS_Beijing_1954",' + 'DATUM["D_Beijing_1954",' + 'SPHEROID["Krasovsky_1940",6378245.0,298.3]],' + 'PRIMEM["Greenwich",0.0],' + 'UNIT["Degree",0.0174532925199433]],' + 'PROJECTION["Transverse_Mercator"],' + 'PARAMETER["False_Easting",-3457147.81],' + 'PARAMETER["False_Northing",0.0],' + 'PARAMETER["Central_Meridian",121.2751921],' + 'PARAMETER["Scale_Factor",1.0],' + 'PARAMETER["Latitude_Of_Origin",0.0],' + 'UNIT["Meter",1.0]]'
    });

    //设置初始范围
    initExtent = new Extent();
    initExtent.xmin = -96772;
    initExtent.xmax = 110772;
    initExtent.ymin = -28592;
    initExtent.ymax = 30000;
    initExtent.setSpatialReference(shanghaiSpatialRef);

    map = new Map("map", {
        extent: initExtent,
        logo: false,
        showAttribution: false,
        showLabels: true,
        minZoom: 1,
        // slider:false,
        sliderPosition: "bottom-left",
        // optimizePanAnimation:false

    });
    map.setZoom(1);
    //设置infoWindow显示位置及偏移量
    // map.infoWindow.anchor = "top-right";
    map.infoWindow.anchor = "top";
    // map.infoWindow.anchor="right";
    map.infoWindow.offsetY = 35;

    //绑定周边查询按钮的点击事件，因为每次周边查询按钮都是新生成的，不需要先解除绑定
    map.infoWindow.on("show", function () {
        $(`#aroundSearchBtn`).click(function () {
            initAroundSearchPanel()
            doNearQuery(aroundSearchCenterGraphic)
        });
    })

    var home = new HomeButton({
        map: map
    }, "HomeButton");
    home.startup();

    initAroundSearchPanel = function () {
        $(".aroundSearchPanel").show();
        $(".addressSearchPanel").hide();
        // hideAllDistrictLayer()
        $(".addressSearchBtn").hide();
        $(".aroundSearchPanel_left_item:first").addClass("active").siblings().removeClass("active");
        $(".el-tabs__active-bar").css("transform", `translateY(0px)`)

        if (isAddressSearch) {
            // $("#aroundSearchClose").hide();
            // $("#aroundSearchBack").show();
        }
    }

    const base = new ArcGISTiledMapServiceLayer(baseMapUrl, {
        "id": "zw",
        "visible": true
    });
    base.setScaleRange(500000, 20000);

    const anse = new ArcGISTiledMapServiceLayer(anseMapUrl3, {
        "id": "anse3",
        "visible": true
    });
    anse.setScaleRange(20000, 500);
    map.addLayers([base, anse]);

    const yingxiang = new ArcGISTiledMapServiceLayer(airUrl, {
        "id": "影像地图",
        "visible": false
    });
    yingxiang.setScaleRange(500000, 500);

    // map.addLayer(yingxiang);

    const zhuji = new ArcGISTiledMapServiceLayer(zhujiUrl, {
        "id": "影像注记",
        "visible": false
    });
    zhuji.setScaleRange(500000, 500);

    const yx = [yingxiang, zhuji];
    map.addLayers(yx);

    //花博会无人机影像
    window.huaboAir = new ArcGISTiledMapServiceLayer(huaboAirUrl, {
        "id": "花博会影像地图",
        "visible": false
    });
    // yingxiang.setScaleRange(500000, 500);

    map.addLayer(window.huaboAir);

    function createDrawDataMenu() {
        var curBox
        ctxMenuForDrawData = new Menu({
            onOpen: function (box) {
                //当前点的位置
                currentLocation = getMapPointFromMenuPosition(box);
                map.infoWindow.hide();
                curBox = box
                // console.log(box)
                // editToolbar.deactivate();
            }
        });
        //添加子菜单栏
        ctxMenuForDrawData.addChild(new MenuItem({
            label: ` <i class="fa fa-search"></i>&nbsp&nbsp周边查询`,
            onClick: function (evt) {
                currentLocation = getMapPointFromMenuPosition(curBox);
                // console.log(currentLocation)
                const graphic = new Graphic(currentLocation)
                initAroundSearchPanel()
                doNearQuery(graphic)
            }
        }));

        //小部件启动
        ctxMenuForDrawData.startup();
        ctxMenuForDrawData.bindDomNode(map.container);
    }

    function getMapPointFromMenuPosition(box) {
        var x = box.x,
            y = box.y;
        switch (box.corner) {
            case "TR":
                x += box.w;
                break;
            case "BL":
                y += box.h;
                break;
            case "BR":
                x += box.w;
                y += box.h;
                break;
        }
        var screenPoint = new Point(x - map.position.x, y - map.position.y);
        return map.toMap(screenPoint);
    }

    map.on("load", function () {
        initAroundSearch()
        initAddressSearch()
        initHotSpot()
        createDrawDataMenu()
        setTimeout(function () {
            initDistrict()
            initFeatureLayer()
            // initImageCompare()
        }, 100)
        // showFeature()
    })

    map.on("extent-change", function (e) {
        window.curCenter = e.extent.getCenter()
        window.curLevel = e.lod.level
        maps[0].centerAndZoom(window.curCenter, window.curLevel - 1);
    })

    map.on("click", function (e) {
        $(".dijitMenuPopup").hide()
    })

    $("#curAirBtn").click(function () {
        yingxiang.show();
        zhuji.show();
        base.hide();
        anse.hide();
        $("#zwBtn").css("display", "flex");
        $("#curAirBtn").hide();
    });

    $("#zwBtn").click(function () {
        yingxiang.hide();
        zhuji.hide();
        base.show();
        anse.show();
        $("#curAirBtn").show();
        $("#zwBtn").hide();
    });

    $("#historyAirBtn").click(function () {
        $("#mainMap").hide();
        $("#compareMap").show()
        maps[0].centerAndZoom(window.curCenter, window.curLevel - 1);

    });

    $(".close").click(function () {
        closePanel();
    });


    setTimeout(() => {
        $(".calcite .esriPopup .titleButton.close").click(function () {
            map.graphics.clear();
            hotspotGraphicLayer.clear()
        });
    }, 2000)

    $("#ieBtn").click(function () {
        // let aurl = 'http://localhost:8080/yjyh/main.html'
        // let aurl = 'http://10.87.94.52/smimap/3dmap/'
        let aurl = 'http://localhost:8080/3dmap/'
        // window.location.href = "openIEFull:" + aurl; // 使用IE打开
        // window.location.href = "openIE:" + aurl; // 使用IE打开
        let url = 'http://10.87.94.52/smimap/test/'
        window.location.href = "alertFull:" + url; // 使用IE打开
    });

    $("#ieBtn2").click(function () {
        let url = 'http://10.87.94.52/smimap/test/'
        window.location.href = "openIE64:" + url; // 使用IE打开
    });

    $(".input_microphone").click(function () {
        // window.location.href = "voice:" // 使用IE打开
        $("#menpaiInput").select();
        document.execCommand('Copy');
        $("#menpaiInput").focus()

        document.execCommand("paste", "false", null);
    });

    resetBtnPosition = function () {
        $("#districtBtn").css("left", "500px");
        $("#districtPanel").css("left", "500px");
        $("#aroundBtn").css("left", "600px");
        $("#aroundTipsPanel").css("left", "710px");
    }



    closePanel = function () {
        isAddressSearch = false;
        if (addressGraphicLayer) {
            addressGraphicLayer.show();
            addressGraphicLayer.clear();
        }
        if (aroundGraphicLayer) {
            aroundGraphicLayer.clear();
        }
        if (hotspotGraphicLayer) {
            hotspotGraphicLayer.clear();
        }
        if (circleGraphicLayer) {
            circleGraphicLayer.clear();
        }

        map.infoWindow.hide();
        map.graphics.clear();

        hideAllDistrictLayer()

        $(".addressSearchBtn").css("display", "flex");
        $("#addressSearchPanel").hide();
        $("#addressSearchPanel input").val("");
        $("#addressPage").empty();
        $("#addressList>ul").empty();
        $("#historyaAdressList").hide()
        $("#addressList .waitPanel").hide();


        $(".aroundSearchPanel").hide();
        $("#aroundPage").empty();
        $("#aroundList ul").empty();

        $("#districtBtn").css("left", "75px");
        $("#districtPanel").css("left", "75px");

        $("#aroundBtn").css("left", "175px");
        $("#aroundTipsPanel").css("left", "285px");

        // $("#aroundSearchClose").show();
        // $("#aroundSearchBack").hide();

        $(".aroundSearchPanel_left_item:first").addClass("active").siblings().removeClass("active");
        $(".el-tabs__active-bar").css("transform", `translateY(0px)`)
        // $(".featureListItem").siblings().removeClass("active");
        clearRoomList()
    }
})